﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using System.Drawing;
using OpenTK.Graphics;
using System.Diagnostics;

namespace TestProj {
    using System.Windows.Forms;
    class MyGameWindow : GameWindow {
        public MyGameWindow() {
            this.Load += MyGameWindow_Load;
            this.Resize += MyGameWindow_Resize;
            this.RenderFrame += MyGameWindow_RenderFrame;
            this.Unload += MyGameWindow_Unload;
            this.Width = 1280;
            this.Height = 720;
            this.MouseMove += MyGameWindow_MouseMove;
        }


        int mouseX, mouseY;
        private void MyGameWindow_MouseMove(object sender, MouseMoveEventArgs e) {
            mouseX = e.X;mouseY = e.Y;
        }

        private void MyGameWindow_RenderFrame(object sender, FrameEventArgs e) {
            GL.ClearColor(.1f, .1f, .99f, .9f);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            Render();

            SwapBuffers();
            ProcessEvents();
        }

        private void MyGameWindow_Load(object sender, EventArgs e) {

            VSync = VSyncMode.Off;
            GL.Enable(EnableCap.DepthTest);
        }

        //MouseDevice mouseDev = new MouseDevice();
        private void Render() {
            MouseState state = this.Mouse.GetState();
            var cursorPosition = System.Windows.Forms.Cursor.Position;
            var clientPos=PointToClient(cursorPosition);
            var cursorState=Mouse.GetCursorState();
            var clampPos = new Point(MathHelper.Clamp(clientPos.X, 0, this.Width),
                                    MathHelper.Clamp(clientPos.Y, 0, this.Height));
            
            Console.WriteLine($"mouseState:{state.X},{state.Y} cursorState:{cursorState.X},{cursorState.Y} gamewindow.Mouse:{this.Mouse.X},{this.Mouse.Y}\t windows cursor:{cursorPosition}\t clientPos:{clientPos}\t clampedPos:{clampPos}  mouseMove:{mouseX},{mouseY}");
            
            //Debug.Assert(state.X == this.Mouse.X && state.Y == this.Mouse.Y, "mouse not equal");
        }

        private void MyGameWindow_Resize(object sender, EventArgs e) {
            GL.Viewport(0, 0, Width, Height);
        }



        private void MyGameWindow_Unload(object sender, EventArgs e) {
            //shader.Dispose();
        }

    }
}
